<?php
declare(strict_types=1);

namespace App\Service\Message;

use Hyperf\Utils\ApplicationContext;
use Hyperf\WebSocketServer\Sender;

class SendPlayerScreenMsgService
{
    
    public function pushPlayerScreenMsg($screen_id, $message)
    {
        $fds = redis()->sMembers(config('cache.prefix')."SCREEN_ID_SOCKET_$screen_id");
        info('fds', [$fds]);
        $sender = new \Hyperf\WebSocketServer\Sender(ApplicationContext::getContainer());
        $res = [];
        if ($fds) {
            try {
                foreach ($fds as $fd) {
                    $res = $sender->push((int)$fd, json_encode($message));
                }
            } catch (\Exception $e) {
                $res = $e->getMessage();
            }
        }

        return $res;
    }
}